<?php
namespace app\admin\controller;

class Food extends Common
{
    public function food_list()
    {   
        $ret = db('food')->order("addtime desc")->paginate(5);
        $pageHtml = $ret->render();
        $this->assign('ret',$ret);
        $this->assign('pageHtml',$pageHtml);
        $this->assign('pick',4);
        return view('food_list');
    }

    public function type_list()
    {   
        $ret = db('food_type')->order("addtime desc")->paginate(5);
        $pageHtml = $ret->render();
        $this->assign('ret',$ret);
        $this->assign('pageHtml',$pageHtml);
        $this->assign('pick',11);
        return view('type_list');
    }

    public function add_food(){
        $type_list = db('food_type')->select();
        $this->assign('type_list',$type_list);
        $this->assign('pick',4);
        return view('add_food');
    }

    public function add_food_action(){
        $name     = input('post.name');
        $price    = input('post.price');
        $stock    = input('post.stock');
        $type     = input('post.type');
        $file = request()->file('food_img');
        $img_src = "";
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
               $img_src = '/uploads/'.$info->getSaveName();
            }
            else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }

        $data = [
            'name'        => $name,
            'price'       => $price,
            'stock'       => $stock,
            'type'        => $type,
            'img'         => $img_src,
            'addtime'     => time()
        ]; 
        
        $ret = db('food')->insert($data);
        if($ret ==false)
        {
            $this->error("添加餐品失败！");
        }
        $this->success("添加餐品成功！",'/admin/food/food_list');
    }

    public function modify_food(){
        $food_id = $_GET['food_id'];
        $food_info = db('food')->where("food_id = {$food_id}")->find();
        $type_list = db('food_type')->select();
        $this->assign('type_list',$type_list);
        $this->assign('food_info',$food_info);
        $this->assign('pick',4);
        return view('modify_food');
    }

    public function modify_food_action(){
        $food_id     = input('post.food_id');
        $name     = input('post.name');
        $price    = input('post.price');
        $stock    = input('post.stock');
        $type     = input('post.type');
        $file = request()->file('food_img');
        $img_src = "";
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
               $img_src = '/uploads/'.$info->getSaveName();
            }
            else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
        
        if(!$img_src)
        {
            $data = [
                'name'        => $name,
                'price'       => $price,
                'stock'       => $stock,
                'type'        => $type,
                'addtime'     => time()
            ]; 
        }
        else
        {
            $data = [
                'name'        => $name,
                'price'       => $price,
                'stock'       => $stock,
                'type'        => $type,
                'img'         => $img_src,
                'addtime'     => time()
            ];      
        }

        $ret = db('food')->where("food_id={$food_id}")->update($data);
        if($ret ==false)
        {
            $this->error("修改餐品失败！");
        }
        $this->success("修改餐品成功！",'/admin/food/food_list');
    }

    public function food_del()
    {
        $food_id = input('get.food_id');
        $ret = db('food')->where("food_id={$food_id}")->delete();
        if(!$ret)
        {
            return [
                'msg' => '删除失败',
                'status' => 0
            ];
        }
        return [
            'msg' => '删除成功',
            'status' => 1
        ];
    }

    public function type_del()
    {
        $type_id = input('get.type_id');
        $ret = db('food_type')->where("type_id={$type_id}")->delete();
        if(!$ret)
        {
            return [
                'msg' => '删除失败',
                'status' => 0
            ];
        }
        return [
            'msg' => '删除成功',
            'status' => 1
        ];
    }
    
    public function add_type(){
        $this->assign('pick',11);
        return view('add_type');
    }

    public function add_type_action(){
        $type_name     = input('post.type_name');

        $data = [
            'type_name'     => $type_name,
            'addtime'       => time()
        ]; 
        
        $ret = db('food_type')->insert($data);
        if($ret ==false)
        {
            $this->error("添加类型失败！");
        }
        $this->success("添加类型成功！",'/admin/food/type_list');
    }

}
